package com.qf.timer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.admin.domain.Jpush;
import com.qf.admin.domain.OrdersMini;
import com.qf.admin.domain.SendMessage;
import com.qf.admin.mapper.JpushMapper;
import com.qf.admin.mapper.MessageMapper;
import com.qf.admin.mapper.OrdersMiniMapper;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author He
 * @Date: 2020/10/23 17:30
 * @Description :
 */
@Component
public class JudgeOrderTime {

    private OrdersMiniMapper ordersMiniMapper;
    private JpushMapper jpushMapper;
    private MessageMapper messageMapper;

    public JudgeOrderTime(OrdersMiniMapper ordersMiniMapper, JpushMapper jpushMapper, MessageMapper messageMapper) {
        this.ordersMiniMapper = ordersMiniMapper;
        this.jpushMapper = jpushMapper;
        this.messageMapper = messageMapper;
    }


    private Logger logger = Logger.getLogger(MessageDelay.class);

    public void judgeOrder() {
        logger.info("判断消息未支付订单时间");
        Date begin = new Date(System.currentTimeMillis() - 86400000);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.lt("o_create_time", begin);
        wrapper.eq("o_status", 0);
        List<OrdersMini> orders = ordersMiniMapper.selectList(wrapper);
        System.out.println(orders);
        if (null != orders) {
            for (OrdersMini order : orders) {
                order.setStatus(2);
                QueryWrapper<OrdersMini> queryWrapper = new QueryWrapper();
                queryWrapper.eq("o_id", order.getOId());
                ordersMiniMapper.update(order, queryWrapper);
            }

        }
    }

    public void judgeOrderTimeBefore() {
        String title = "你有订单即将过期";
        logger.info("判断消息未支付订单时间提醒用户");
        QueryWrapper<OrdersMini> wrapper = new QueryWrapper();
        //22小时 = 79200000 lt 小于 : begin < o_create_time
        Date begin = new Date(System.currentTimeMillis() - 86400000);
        Date end = new Date(System.currentTimeMillis() - 79200000);
        wrapper.gt("o_create_time", begin);
        wrapper.lt("o_create_time", end);
        wrapper.eq("o_status", 0);
        List<OrdersMini> orders = ordersMiniMapper.selectList(wrapper);
        if (null != orders) {
            for (OrdersMini order : orders) {
                String time = convertMillis(order.getCreateTime().getTime() - System.currentTimeMillis() + 86400000);
                String content = order.getTitle() + "还有" + time + "过期!请尽快购买!";
                Map<String, String> parms = new HashMap<>(8);
                // Jpush jpush = jpushMapper.selectById(29);
                Jpush jpush = jpushMapper.selectById(order.getUserId());
                parms.put("id", jpush.getRegId());
                parms.put("msg", content);
                MessageDelay.jpushAndroid(parms, title);

                SendMessage sendMessage = new SendMessage();
                sendMessage.setUserId(jpush.getUserId());
                sendMessage.setContent(content);
                sendMessage.setCreateTime(new Date());
                sendMessage.setTitle(title);
                sendMessage.setName("系统自动");
                sendMessage.setStatus(1);
                messageMapper.insert(sendMessage);
            }
        }
    }

    public String convertMillis(long millis) {

        long totalSeconds = millis / 1000;
        long currentSeconds = totalSeconds % 60;
        long totalMinutes = totalSeconds / 60;
        long currentMintes = totalMinutes % 60;
        long totalHours = totalMinutes / 60;
        return totalHours + ":" + currentMintes + ":" + currentSeconds;
    }

}
